package com.yyk.yykywd.ui.work;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import androidx.annotation.Nullable;

import android.view.View;
import android.widget.Button;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;
import com.orhanobut.hawk.Hawk;
import com.orhanobut.logger.Logger;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.yyk.yykywd.BusinessPeopleInfoKt;
import com.yyk.yykywd.R;
import com.yyk.yykywd.base.BaseWebActivity;
import com.yyk.yykywd.bean.CenterCarInfo;
import com.yyk.yykywd.config.HttpUrl;
import com.yyk.yykywd.http.Api;
import com.yyk.yykywd.http.ApiService;
import com.yyk.yykywd.util.Md5Util2;
import com.yyk.yykywd.util.WxShareUtils;

import java.util.Map;
import java.util.TreeMap;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;


/**
 * 名片卡片分享网页
 */
public class CardWebActivity extends BaseWebActivity implements View.OnClickListener {

    Button btRevise;
    Button btSharewx;
    Button btSharepyq;

    private String position;

    @Override
    public void initView() {
        setBackArrow();
        setTitle("对外名片");
        Intent intent = getIntent();

        Map<String, String> map = new TreeMap<>();
        map.put("uid", BusinessPeopleInfoKt.getUid());
        map.put("sign", Md5Util2.createSign(map));
        ApiService.getInstance().api.postCenterCar(map)
                .subscribeOn(Schedulers.io())     //调度线程
                .observeOn(AndroidSchedulers.mainThread())
                .unsubscribeOn(Schedulers.io())   //取消订阅
                .subscribe(new Observer<CenterCarInfo>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(CenterCarInfo info) {
                        Logger.e("我的名片" + info.toString());
//                        ToastUtil.showShort(info.getMsg());
                        if (info.getCode() == 1) {
                            position = info.getData().getPosition();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 处理穿过来的网址
     *
     * @return
     */
    @Override
    public String getUrl() {
        return HttpUrl.BASE_H5_URL + Api.NAMECARD + BusinessPeopleInfoKt.getUid();
    }

    @Override
    protected void onResume() {
        super.onResume();
        //刷新
        mAgentWeb.getWebCreator().getWebView().reload();
        //测试Cookies
        /*try {
            String targetUrl="";
            Log.i("Info","cookies:"+ AgentWebConfig.getCookiesByUrl(targetUrl="http://www.jd.com"));
            AgentWebConfig.removeAllCookies(new ValueCallback<Boolean>() {
                @Override
                public void onReceiveValue(Boolean value) {
                    Log.i("Info","onResume():"+value);
                }
            });
            String tagInfo=AgentWebConfig.getCookiesByUrl(targetUrl);
            Log.i("Info","tag:"+tagInfo);
            AgentWebConfig.syncCookie("http://www.jd.com","ID=IDHl3NVU0N3ltZm9OWHhubHVQZW1BRThLdGhLaFc5TnVtQWd1S2g1REcwNVhTS3RXQVFBQEBFDA984906B62C444931EA0");
            String tag=AgentWebConfig.getCookiesByUrl(targetUrl);
            Log.i("Info","tag:"+tag);
            AgentWebConfig.removeSessionCookies();
            Log.i("Info","removeSessionCookies:"+AgentWebConfig.getCookiesByUrl(targetUrl));
        }catch (Exception e){
            e.printStackTrace();
        }*/

    }

    @Override
    public View getBottomView() {
        View view = View.inflate(mActivity, R.layout.view_bottom_card, null);
        btRevise = view.findViewById(R.id.bt_revise);
        btSharewx = view.findViewById(R.id.bt_sharewx);
        btSharepyq = view.findViewById(R.id.bt_sharepyq);

        btRevise.setOnClickListener(this);
        btSharewx.setOnClickListener(this);
        btSharepyq.setOnClickListener(this);
        return view;
    }

    @Override
    protected void onStart() {

        super.onStart();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_revise:
                startAct(ReviseCardActivity.class);
                break;
            case R.id.bt_sharewx:
                Glide.with(this).asBitmap().load(R.mipmap.wxfenxiang).into(new SimpleTarget<Bitmap>() {
                    /**
                     * 成功的回调
                     */
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                        // 下面这句代码是一个过度dialog，因为是获取网络图片，需要等待时间
//                        mDialog.dismiss();
                        // 调用方法
                        WxShareUtils.shareWeb(mActivity, "wx9b11db1afb32aaae",
                                getUrl(), Hawk.get("name") + "  " + position, "云医康专属个人名片",
                                bitmap, SendMessageToWX.Req.WXSceneSession);
                    }

                    /**
                     * 失败的回调
                     */
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
//                        mDialog.dismiss();

                        WxShareUtils.shareWeb(mActivity, "wx9b11db1afb32aaae",
                                getUrl(), Hawk.get("name") + "  " + position, "云医康专属个人名片",
                                null, SendMessageToWX.Req.WXSceneTimeline);
                    }
                });

                break;
            case R.id.bt_sharepyq:
                Glide.with(this).asBitmap().load(R.mipmap.wxfenxiang).into(new SimpleTarget<Bitmap>() {
                    /**
                     * 成功的回调
                     */
                    @Override
                    public void onResourceReady(Bitmap bitmap, Transition<? super Bitmap> transition) {
                        // 下面这句代码是一个过度dialog，因为是获取网络图片，需要等待时间
//                        mDialog.dismiss();
                        // 调用方法
                        WxShareUtils.shareWeb(mActivity, "wx9b11db1afb32aaae",
                                getUrl(), Hawk.get("name") + "  " + position, "云医康专属个人名片",
                                bitmap, SendMessageToWX.Req.WXSceneTimeline);
                    }

                    /**
                     * 失败的回调
                     */
                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
//                        mDialog.dismiss();

                        WxShareUtils.shareWeb(mActivity, "wx9b11db1afb32aaae",
                                getUrl(), Hawk.get("name") + "  " + position, "云医康专属个人名片",
                                null, SendMessageToWX.Req.WXSceneFavorite);
                    }
                });
                break;

        }
    }
}
